package com.server.modules.api.web;


import com.server.common.utils.StringUtils;
import com.server.modules.api.Base.BaseAppController;
import com.server.modules.api.service.IUsedCarEvaluateService;
import com.server.modules.api.vo.BaseQueryVo;
import com.server.modules.sys.entity.juhe.usedcar.JuheUsedcarBrand;
import com.server.modules.sys.entity.juhe.usedcar.JuheUsedcarModel;
import com.server.modules.sys.entity.juhe.usedcar.JuheUsedcarProvince;
import com.server.modules.sys.entity.juhe.usedcar.JuheUsedcarSeries;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 工具箱接口
 * Created by Administrator on 2016/7/22.
 */
@Controller
@RequestMapping(value = "/api/toolbox")
public class ToolboxAPIController extends BaseAppController {
	@Resource
	private IUsedCarEvaluateService usedCarEvaluateService;//二手车估值接口
	@ResponseBody
	@RequestMapping(value = "/getAllProvince")
	public Object  getAllProvince(){
		return usedCarEvaluateService.getAllProvince();
	}
	@ResponseBody
	@RequestMapping(value = "/getCity")
	public Object  getCity(String provinceId)
	{
		return usedCarEvaluateService.getCityByProvince(provinceId);
	}
	@ResponseBody
	@RequestMapping(value = "/getCarBrandList")
	public Object  getCarBrandList(String vehicle){
		return usedCarEvaluateService.getCarBrandList(vehicle);
	}
    @RequestMapping(value = "")
    public String  main(BaseQueryVo vo,Model model){
        try {
            if (!checkLogin(vo)) {
                return "/error/500";
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return "/error/500";
        }
        model.addAttribute("memberId",vo.getMemberId());
        model.addAttribute("sessionId",vo.getSessionId());
        return "/modules/api/toolbox/toolbox";
    }
	@ResponseBody
	@RequestMapping(value = "/getCarSeriesList")
	public Object  getCarSeriesList(String brandId){
        Map<String,List<JuheUsedcarSeries>>  typeSeries=new TreeMap();
        List<JuheUsedcarSeries> carSeriesList = usedCarEvaluateService.getCarSeriesList(brandId);
        for (JuheUsedcarSeries juheUsedcarSeries : carSeriesList) {
            String ppname = juheUsedcarSeries.getPpname();
            if(typeSeries.containsKey(ppname)){
                typeSeries.get(ppname).add(juheUsedcarSeries);
            }else{
                List<JuheUsedcarSeries>temps=new ArrayList<>();
                temps.add(juheUsedcarSeries);
                typeSeries.put(ppname,temps);
            }
        }
        return typeSeries;
	}
	@ResponseBody
	@RequestMapping(value = "/getCarModelList")
	public Object  getCarModelList(String seriesId){
        Map<String,List<JuheUsedcarModel>>  typeModels=new TreeMap();
        List<JuheUsedcarModel> carModelList = usedCarEvaluateService.getCarModelList(seriesId);
        for (JuheUsedcarModel carModel : carModelList) {
            String tYear = carModel.getTyear();
            if(typeModels.containsKey(tYear)){
                typeModels.get(tYear).add(carModel);
            }else{
                List<JuheUsedcarModel>temps=new ArrayList<>();
                temps.add(carModel);
                typeModels.put(tYear,temps);
            }
        }
        return typeModels;
	}

	@ResponseBody
	@RequestMapping(value = "/getCarYearList")
	public Object  getCarYearList(String carId){

		return usedCarEvaluateService.getCarYearList( carId);
	}


	@RequestMapping(value = "/evaluatePage")
	public ModelAndView evaluatePage( BaseQueryVo vo){
        ModelAndView mv = new ModelAndView();
       try {
            if (!checkLogin(vo)) {
                mv.setViewName("/error/500");
                return mv;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            mv.setViewName("/error/500");
            return mv;
        }
        mv.addObject("memberId",vo.getMemberId());
        mv.addObject("sessionId",vo.getSessionId());
        List<JuheUsedcarProvince> allProvince = usedCarEvaluateService.getAllProvince();
        mv.addObject("allProvince",allProvince);
        List<JuheUsedcarBrand> brands = usedCarEvaluateService.getCarBrandList("passenger");
        Map<String,List<JuheUsedcarBrand>>  typeBrands=new TreeMap();
        for (JuheUsedcarBrand brand : brands) {
            String pin = brand.getPin();
            if(typeBrands.containsKey(pin)){
                typeBrands.get(pin).add(brand);
            }else{
                List<JuheUsedcarBrand>temps=new ArrayList<>();
                temps.add(brand);
                typeBrands.put(pin,temps);
            }
        }
        mv.addObject("typeBrands",typeBrands);
        mv.setViewName("/modules/api/toolbox/usedcar/evaluate");
		return mv;
	}

    @RequestMapping(value = "/doEvaluation")
    @ResponseBody
    public Object doEvaluation(String provinceId, String cityId, String carId, String useddate, String useddateMonth, String mileage, String price, HttpServletRequest request,Model model,BaseQueryVo vo){
      HashedMap ret=new HashedMap();
      try {
            if (!checkLogin(vo)) {
                ret.put("succeed",false);
                ret.put("msg","估值失败，获取登陆信息失败");
                return ret;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            ret.put("succeed",false);
            ret.put("msg","估值失败，服务器错误");
            return ret;
        }
        if(StringUtils.isBlank(provinceId)||StringUtils.isBlank(cityId)||StringUtils.isBlank(carId)||useddate==null||StringUtils.isBlank(useddateMonth)||mileage==null||price==null){
            ret.put("succeed",false);
            ret.put("msg","估值失败，服务器错误");
            return ret;
        }
        double aDouble = Double.valueOf(mileage);
        int mileageInt= (int) aDouble;

        double aprice = Double.valueOf(price);
        int priceInt= (int) aprice;

        return  usedCarEvaluateService.doEvaluation(provinceId,cityId,carId,useddate, useddateMonth,mileageInt,priceInt,request,vo);
    }



}
